<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div class="wrap">
        <h2 id="title">模拟封装jqeury</h2>
        <ul class="list">
            <li>01</li>
            <li>02</li>
            <li>03</li>
            <li>04</li>
            <li>05</li>
            <li>06</li>
        </ul>
    </div>
</body>
<script>

    // 获取元素 根据传入的css选择器得到一个伪数组,可以使用GetEle.prototype的属性和方法
    function GetEle(selector) {
        var list = document.querySelectorAll(selector);

        list = Array.from(list);  // 转为真数组 
        Object.setPrototypeOf(list, GetEle.prototype);  // 再改为伪数组

        return list; // [ele,ele,ele,ele]
    }

    // prototype 原型对象方法  => 所有的实例化对象均可使用
    GetEle.prototype.setText = function (con) {
        // this => 指向调用此方法的实例化对象  // 伪数组  [li,li,li,li]
        for (var i = 0; i < this.length; i++) {
            var ele = this[i];
            ele.textContent = con;
        }
    }

    GetEle.prototype.getText = function (con) {
        // this => 指向调用此方法的实例化对象  // 伪数组  [li,li,li,li]
        return this[0].textContent;
    }

    var $li = new GetEle(".list li");
    console.log($li);
    // $li.setText(11111);
    // console.log($li.getText());

    var $title = new GetEle("#title");
    // console.log($title);
    // $title.setText(22222);
    // console.log($title.getText());






    /*  function setText(con) {
         for (var i = 0; i < list.length; i++) {
             var ele = list[i];
             ele.textContent = con;
         }
     }
 
     function getText(con) {
         // var arr = [];
         // for (var i = 0; i < list.length; i++) {
         //     var ele = list[i];
         //     arr.push(ele.textContent);
         // }
         // return arr; 
 
         return list[0].textContent;
     }
 
     function addClass(classStr) { // 一个或多个(多个用空格分隔 "one two three")
         var arr = classStr.split(" "); // 
         for (var i = 0; i < list.length; i++) {
             var ele = list[i];
             ele.classList.add(...arr);
         }
     }
 
     function removeClass(classStr) { // 一个或多个(多个用空格分隔 "one two three")
         var arr = classStr.split(" "); // 
         for (var i = 0; i < list.length; i++) {
             var ele = list[i];
             ele.classList.remove(...arr);
         }
     } */










</script>

</html>